package com.link510.aitools.core.helper;

import android.util.Log;

import com.orhanobut.logger.Logger;

/**
 * Created by Admin on 2017/12/13.
 */

public class TypeHelper {


    private static final String TAG = "TypeHelper";

    private static TypeHelper mInstance = null;

    public synchronized static TypeHelper getInstance() {

        if (mInstance == null) {

            synchronized (TypeHelper.class) {

                if (mInstance != null) {
                    return mInstance;
                }

                mInstance = new TypeHelper();
            }
        }

        return mInstance;
    }

    private TypeHelper() {
        Logger.e("u can initializing me...");
    }


    //把long类型的毫秒转换为int类型的秒
    public int timeMsToSecond(long ms) {

        int ms2 = Long.valueOf(ms).intValue();

        return ms2 / 1000;
    }

    public int longToInt(long value) {

        return Long.valueOf(value).intValue();
    }

    public String intToString(int i) {

        String s = i + "";
        return s.trim();

    }

    public int stringToInt(Object s) {
        return stringToInt(s, 0);
    }


    /// <summary>
    /// 将string类型转换成int类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public int stringToInt(Object s, int defaultValue) {
        try {

            if (s != null && String.valueOf(s).trim().length() > 0) {
                return Integer.parseInt(s.toString());
            } else {
                return defaultValue;
            }

        } catch (Exception e) {
            Log.e("ERROR", e.toString());
            return defaultValue;
        }
    }

    public int objectToInt(Object o) {

        return objectToInt(o, 0);
    }

    public int objectToInt(Object o, int defaultValue) {
        try {
            return (int) o;
        } catch (Exception e) {
            return defaultValue;
        }
    }


    /// <summary>
    /// 将string类型转换成int类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public String intToString(Object s, String defaultValue) {
        try {

            if (s != null) {
                return String.valueOf(s.toString());
            } else {
                return defaultValue;
            }

        } catch (Exception e) {
            Log.e("ERROR", e.toString());
            return defaultValue;
        }
    }

    /**
     * char 转字符串
     *
     * @param c
     * @return
     */
    public String charToString(char c) {
        try {
            return String.valueOf(c);
        } catch (Exception e) {
            return "";
        }
    }


    /// <summary>
    /// 将int类型转换成string类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public long stringToLong(String s) {
        return stringToLong(s, 0);
    }

    /// <summary>
    /// 将int类型转换成string类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public long stringToLong(String s, long defaultValue) {
        try {

            if (s != null) {
                return Long.parseLong(s.toString());
            } else {
                return defaultValue;
            }

        } catch (Exception e) {
            Log.e("ERROR", e.toString());
            return defaultValue;
        }
    }

    /// <summary>
    /// 将int类型转换成string类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public String longToString(long s) {
        return longToString(s, "0");
    }

    /// <summary>
    /// 将string类型转换成int类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public String longToString(Object s, String defaultValue) {
        try {

            if (s != null) {
                return String.valueOf(s.toString());
            } else {
                return defaultValue;
            }

        } catch (Exception e) {
            Log.e("ERROR", e.toString());
            return defaultValue;
        }
    }

    /// <summary>
    /// 将string类型转换成double类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public double stringToDouble(String s) {
        return stringToDouble(s, 0);
    }

    /// <summary>
    /// 将string类型转换成double类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public double stringToDouble(Object s, int defaultValue) {
        try {

            if (s != null) {
                return Double.parseDouble(s.toString());
            } else {
                return defaultValue;
            }

        } catch (Exception e) {
            Log.e("ERROR", e.toString());
            return defaultValue;
        }
    }


    /// <summary>
    /// 将string类型转换成double类型
    /// </summary>
    /// <param name="s">目标字符串</param>
    /// <returns></returns>
    public String doubleToString(double s) {
        return doubleToString(s, "");
    }

    public String doubleToString(Object s, String defaultValue) {

        try {

            if (s != null) {
                return String.valueOf(s.toString());
            } else {
                return defaultValue;
            }

        } catch (Exception e) {
            Log.e("ERROR", e.toString());
            return defaultValue;
        }

    }


    public int longToInt(Long value) {
        return longToInt(value, 0);
    }

    public int longToInt(Long value, int defaultValue) {
        try {
            return value.intValue();
        } catch (Exception e) {

            return defaultValue;
        }
    }


    public int doubleToInt(double value) {
        return doubleToInt(value, 0);
    }

    public int doubleToInt(double value, int defaultValue) {
        try {
            return Double.valueOf(value).intValue();
        } catch (Exception e) {

            return defaultValue;
        }
    }


}
